package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mr.ali.videoapp.portal.model.CollectionPlay;
import mr.ali.videoapp.portal.model.CollectionPlayWatch;
import mr.ali.videoapp.portal.mapper.CollectionPlayWatchMapper;
import mr.ali.videoapp.portal.service.ICollectionPlayService;
import mr.ali.videoapp.portal.service.ICollectionPlayWatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-07-20
 */
@Service
@Slf4j
public class CollectionPlayWatchServiceImpl extends ServiceImpl<CollectionPlayWatchMapper, CollectionPlayWatch> implements ICollectionPlayWatchService {

    @Resource
    private CollectionPlayWatchMapper collectionPlayWatchMapper;

    @Resource
    private ICollectionPlayService iCollectionPlayService;

    @Scheduled(fixedRate = 1000 * 60 * 60 * 24 * 15)
    public void clearWatch() {
        QueryWrapper<CollectionPlayWatch> wrapper = new QueryWrapper<>();
        collectionPlayWatchMapper.delete(wrapper);
        log.info("清除观看记录");
    }

    /**
     * 展示观看记录
     */
    @Override
    public PageInfo<CollectionPlayWatch> findAllIndex(Integer pageNum, Integer pageSize, Integer userId) {
        QueryWrapper<CollectionPlayWatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("watch_time");
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionPlayWatch> tags = collectionPlayWatchMapper.selectList(queryWrapper);
        for (CollectionPlayWatch video : tags) {
            List<CollectionPlay> videos = iCollectionPlayService.videoTagsById(video.getVideoId());
            video.setCollection(videos);
        }
        return new PageInfo<>(tags);
    }

    //    删除观看记录
    @Override
    public Integer delete(Integer userId, Integer videoId) {
        QueryWrapper<CollectionPlayWatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        return collectionPlayWatchMapper.delete(queryWrapper);
    }
    //    更新观看记录
    @Override
    public void update(Integer userId, Integer urlId , Integer videoId ,Integer Episode){
        CollectionPlayWatch collectionWatch1 = new CollectionPlayWatch();
        QueryWrapper<CollectionPlayWatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        CollectionPlayWatch collectionWatch = collectionPlayWatchMapper.selectOne(queryWrapper);

        if (collectionWatch != null) {
            // 实体类 CollectionWatch 包含 id 和 watchTime 字段
            collectionWatch.setWatchTime(System.currentTimeMillis());
            collectionWatch.setUrlId(urlId);
            collectionWatch.setEpisode(Episode);
            collectionPlayWatchMapper.updateById(collectionWatch);
            log.info("已找到观看记录：" +collectionWatch );
        } else {
            collectionWatch1.setUserId(userId);
            collectionWatch1.setVideoId(videoId);
            collectionWatch1.setEpisode(Episode);
            collectionWatch1.setUrlId(urlId);
            collectionWatch1.setWatchTime(System.currentTimeMillis());
            collectionPlayWatchMapper.insert(collectionWatch1);
            log.info("未找到该用户的观看记录");
        }

    }

}
